package com.example.dell.fastshopping.user.bank;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.utils.KLog;
import com.example.dell.fastshopping.utils.RegexUtils;
import com.example.dell.fastshopping.utils.ToastUtils;
import com.google.gson.GsonBuilder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${YSH} on 2019/5/21.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class AddBankCardActivity extends AppCompatActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.right_text)
    TextView rightText;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.idcard)
    EditText idcard;
    @BindView(R.id.bank_name)
    TextView bankName;
    @BindView(R.id.card_number)
    EditText cardNumber;
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.phone)
    EditText phone;
    private BankModel model;
    private String action;
    private BankCard bankCard;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bankcard);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        title.setText("添加银行卡");
        Intent intent = getIntent();
        action = intent.getStringExtra("action");
        if ("edit".equals(action)) {
            bankCard = (BankCard) intent.getSerializableExtra("entity");
            title.setText("编辑银行卡");
            name.setText(bankCard.getUserName());
            idcard.setText(bankCard.getIdCard());
            bankName.setText(bankCard.getBank());
            cardNumber.setText(bankCard.getCardId());
            phone.setText(bankCard.getPhone());

        }
        model = ViewModelProviders.of(this).get(BankModel.class);

        cardNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    checkBankCard(true);
                }
            }
        });
        cardNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                KLog.e("sss");
                int length = cardNumber.getText().toString().length();
                if (length == 16 || length == 17 || length == 19) {
                    checkBankCard(false);
                }
            }
        });

    }

    private void checkBankCard(final boolean b) {
        String cardN = cardNumber.getText().toString().trim();
        if (cardN.isEmpty()) {
            return;
        }
        model.checkBankCard(cardN).observe(this, new Observer<BaseResponse<String>>() {
            @Override
            public void onChanged(@Nullable BaseResponse<String> response) {
                if (response.isSuccess()) {
                    String data = response.getData();
                    BankCard bankCard = new GsonBuilder().create().fromJson(data, BankCard.class);
                    String bankName1 = bankCard.getBankName();
                    bankName.setText(bankName1);
                } else {
                    if (b) {
                        ToastUtils.showShort("银行有误请检查！");
                        cardNumber.setText("");
                    }
                }
            }
        });
    }

    @OnClick({R.id.back, R.id.confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.confirm:
                addCard();
                break;
        }
    }

    private void addCard() {
        String name = this.name.getText().toString().trim();

        if (name.isEmpty()) {
            ToastUtils.showShort("请填写姓名");
            return;
        }
        String idCard = idcard.getText().toString().trim();

        if (idCard.isEmpty() || !(RegexUtils.isIDCard15(idCard) || RegexUtils.isIDCard18(idCard))) {
            ToastUtils.showShort("请检查身份证号");
            return;
        }

        String phone_ = phone.getText().toString().trim();
        if (!RegexUtils.isMobileSimple(phone_)) {
            ToastUtils.showShort("请检查手机号码");
            return;
        }
        String bankN = bankName.getText().toString().trim();
        if (bankN.isEmpty()) {
            ToastUtils.showShort("请填写所属银行");
            return;
        }

        String cardN = cardNumber.getText().toString().trim();

        if (cardN.isEmpty() || (cardN.length() < 16 && cardN.length() > 19)) {
            ToastUtils.showShort("请检查银行卡号");
            return;
        }

        if ("add".equals(action)) {
            model.addBankCard(idCard, phone_, bankN, name, cardN).observe(this, new Observer<BaseResponse>() {
                @Override
                public void onChanged(@Nullable BaseResponse response) {
                    finish();
                }
            });
        } else {
            model.editBankCard(bankCard.getId(), idCard, phone_, bankN, name, cardN).observe(this, new Observer<BaseResponse>() {
                @Override
                public void onChanged(@Nullable BaseResponse response) {
                    finish();
                }
            });
        }
    }

}
